export * from './FileHandle/index.js' // 文件处理
export * from './TimeHandle/index.js' // 时间处理
export * from './DeviceUuid/index.js' // 设备号
export * from './VerifyHandle/index.js' // 校验处理
export * from './uses/index.js' // uses
export * from './prompts.js' // 首次打开页面的弹窗提示
export * from './resize.js' //
export * from './string/transcode.js'
export * from './statistics.js' //埋点js
export * from './judgePort.js' //埋点js
export * from './storage.js' //缓存
export * from './indexedDB.js' //indexedDB数据库

const addHttp = (url) => {
  let baseUrl =
    location.host == 'localhost:3000' ? '域名' : location.origin
  return url ? (url.includes('http') ? url : `${baseUrl}${url}`) : ''
}
const urlAddHttp = (url) => {
  return url ? (url.includes('http') ? url : `http://${url}`) : ''
}

const delUrlQuery = (url, aParam) => {
  aParam.forEach((item) => {
    const fromindex = url.indexOf(`${item}=`) //必须加=号，避免参数值中包含item字符串
    if (fromindex !== -1) {
      // 通过url特殊符号，计算出=号后面的的字符数，用于生成replace正则
      const startIndex = url.indexOf('=', fromindex)
      const endIndex = url.indexOf('&', fromindex)
      const hashIndex = url.indexOf('#', fromindex)
      let reg
      if (endIndex !== -1) {
        // 后面还有search参数的情况
        const num = endIndex - startIndex
        reg = new RegExp(`${item}=.{${num}}`)
        url = url.replace(reg, '')
      } else if (hashIndex !== -1) {
        // 有hash参数的情况
        const num = hashIndex - startIndex - 1
        reg = new RegExp(`&?${item}=.{${num}}`)
        url = url.replace(reg, '')
      } else {
        // search参数在最后或只有一个参数的情况
        reg = new RegExp(`&?${item}=.+`)
        url = url.replace(reg, '')
      }
    }
    const noSearchParam = url.indexOf('=')
    if (noSearchParam === -1) {
      url = url.replace(/\?/, '') // 如果已经没有参数，删除？号
    }
  })
  return url
}

let url = window.location.href
let dz_url = url.split('#')[0]

const getPublicURl = (fileUrl) => {
  return import.meta.env.VITE_APP_ENV === 'production'
    ? `${dz_url}${fileUrl}`
    : `/${fileUrl}`
}

/**
 *  将数组对象中的空值设置成'-'
 * @param {Array} Arr
 * @returns
 */
const dealObjNullToBar = (Arr) => {
  if (Arr && Arr.length > 0) {
    Arr.map((item) => {
      Object.keys(item).map((key) => {
        if (item[key] === '' || item[key] === null) {
          item[key] = '-'
        }
      })
    })
  }
  return Arr
}

/**
 * 查看对象中的所有值是否为空
 * @param {Object} obj
 * @returns
 */

const isObjValueNull = (obj) => {
  let val = false
  if (obj?.constructor === Object) {
    let len = Object.values(obj).filter((el) => {
      return el !== null && el !== ''
    })
    val = !len.length > 0
  } else {
    val = true
  }
  return val
}
/**
 * 获取当前url的参数
 * @param {String} paramName
 * @param {String} url
 * @returns
 */
const getUrlParameter = (paramName, url) => {
  if (!url) {
    url = window.location.href
  }

  const paramArr = url.slice(url.indexOf('?') + 1).split('&')
  const params = {}
  paramArr.map((param) => {
    const [key, val] = param.split('=')
    params[key] = decodeURIComponent(val)
  })
  return params[paramName]
}

/**
 * 传入日期 返回距离上一年份的年份数组
 * @param {String} from  2020-10-10
 * @returns { Array }  [2020,2021]
 */

const getYeararrByYear = (from) => {
  let arr = []
  if (from) {
    let fromYear = new Date(from).getFullYear()
    let nowYear = new Date().getFullYear()
    if (fromYear < nowYear) {
      arr = Array.from({ length: nowYear - fromYear }).map((item, index) => {
        return fromYear + index
      })
    }
  }
  return arr
}
/**
 * 将blob转换为图片文件
 * @param { blob } fileData
 * @returns file
 */
const blobToImgFile = (fileData) => {
  let fileOfBolb = new File([fileData], new Date().getDate() + '.png')
  return fileOfBolb
}

/**
 * @name 计算比例
 * @description 如果有数为空则返回-
 * @function
 * @param { Number } a
 * @param { Number } b
 * @return { Number }
 */

const calculateScale = (a = '', b = '') => {
  if (!(a && b)) {
    return '-'
  } else {
    let scale =
      a !== '' && b !== '' ? ((Number(a) / Number(b)) * 100).toFixed(2) : 0.0
    return `${scale}%`
  }
}

/**
 * @name 处理文本域回显换行，用于导出excel
 * @description 处理文本域内容 将'\n'然后替换成<br style="mso-data-placement:same-cell;"/>
 * @function
 * @param {String} str
 * @return {String}
 */

const replaceMark = (str) => {
  if (str && str.replace) {
    return str.replace(
      /\n|\r\n/g,
      '<br style="mso-data-placement:same-cell;"/>'
    )
  } else {
    return str
  }
}

export {
  addHttp,
  urlAddHttp,
  delUrlQuery,
  getPublicURl,
  dealObjNullToBar,
  isObjValueNull,
  getUrlParameter,
  getYeararrByYear,
  blobToImgFile,
  calculateScale,
  replaceMark,
}
